package game.Elements;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class Main extends View
{
	public SceneMethod m_Scene;
	private Table m_Table;
	
	private Point m_BackgroundLevel_Table_RenderPoints;
	private Point m_BackgroundLevel_Bottom_0_RenderPoints;
	private Point m_BackgroundLevel_Bottom_1_RenderPoints;
	private Point m_BackgroundLevel_Top_0_RenderPoints;
	private Point m_BackgroundLevel_Pot_RenderPoints;
	
	
	private Bitmap m_BackgroundLevel_Table;
	private Bitmap m_BackgroundLevel_Bottom_0;
	private Bitmap m_BackgroundLevel_Bottom_1;
	private Bitmap m_BackgroundLevel_Top_0;
	private Bitmap m_BackgroundLevel_Pot;

	private long m_LastFrameTime = -1;
	private long m_DeltaTime = -1;
	private long m_TotalElapsedTime = -1;
	private String m_FPSString;
	
	private long m_TouchMode;//0-none,1-pressed,2-released
	private boolean m_CallTouch;
	private float m_TouchX,m_TouchY;
	
	
	public void SetTouch(float X, float Y, int TouchType)
	{
		m_CallTouch = true;
   		m_TouchX = X;
   		m_TouchY = Y;
   		m_TouchMode = TouchType;
	}	
	
	private enum SceneMethod
	{
		LOADING,
		MENU,
		GAME
	}
	
	public Main(Context context)
	{      
		super(context);
		setKeepScreenOn(true);
		m_FPSString = new String();
		
		//DEBUG TEST
		//m_Scene = SceneMethod.LOADING;
		//m_LastFrameTime = System.currentTimeMillis();
		m_Scene = SceneMethod.GAME;
		LoadAndResizeGraphics(context);
	}
   

   
   private void LoadAndResizeGraphics(Context context)
   {
		Resources res = context.getResources();
		Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		m_BackgroundLevel_Table_RenderPoints = new Point();
		m_BackgroundLevel_Bottom_0_RenderPoints = new Point();
		m_BackgroundLevel_Bottom_1_RenderPoints = new Point();
		m_BackgroundLevel_Top_0_RenderPoints = new Point();
		m_BackgroundLevel_Pot_RenderPoints = new Point();
		
		int width = display.getWidth();
		int height = display.getHeight();
		float multiplier;
		//DEBUG TEST
		//width = 240;
		//height = 400;

		m_BackgroundLevel_Table = BitmapFactory.decodeResource(res, R.drawable.baseleveltable);
		multiplier = (float)width / m_BackgroundLevel_Table.getWidth();
		m_BackgroundLevel_Bottom_0 = BitmapFactory.decodeResource(res, R.drawable.baselevelbottom);		
		m_BackgroundLevel_Bottom_1 = BitmapFactory.decodeResource(res, R.drawable.toplevelbottom);		
		m_BackgroundLevel_Pot = BitmapFactory.decodeResource(res, R.drawable.baselevelpot);
		m_BackgroundLevel_Top_0 = BitmapFactory.decodeResource(res, R.drawable.baseleveltop);

		m_BackgroundLevel_Table = Bitmap.createScaledBitmap(
				m_BackgroundLevel_Table, (int)(m_BackgroundLevel_Table.getWidth()*multiplier), (int)(m_BackgroundLevel_Table.getHeight()*multiplier), true);		
		m_BackgroundLevel_Bottom_0 = Bitmap.createScaledBitmap(
				m_BackgroundLevel_Bottom_0, (int)(m_BackgroundLevel_Bottom_0.getWidth()*multiplier), (int)(m_BackgroundLevel_Bottom_0.getHeight()*multiplier), true);		
		m_BackgroundLevel_Bottom_1 = Bitmap.createScaledBitmap(
				m_BackgroundLevel_Bottom_1, (int)(m_BackgroundLevel_Bottom_1.getWidth()*multiplier), (int)(m_BackgroundLevel_Bottom_1.getHeight()*multiplier), true);
		m_BackgroundLevel_Pot = Bitmap.createScaledBitmap(
				m_BackgroundLevel_Pot, (int)(m_BackgroundLevel_Pot.getWidth()*multiplier), (int)(m_BackgroundLevel_Pot.getHeight()*multiplier), true);
		m_BackgroundLevel_Top_0 = Bitmap.createScaledBitmap(
				m_BackgroundLevel_Top_0, (int)(m_BackgroundLevel_Top_0.getWidth()*multiplier), (int)(m_BackgroundLevel_Top_0.getHeight()*multiplier), true);
		
		int tableS;
		tableS = (height - m_BackgroundLevel_Bottom_0.getHeight() - m_BackgroundLevel_Top_0.getHeight()) - m_BackgroundLevel_Table.getHeight();
		m_BackgroundLevel_Table_RenderPoints.y = tableS/2 + m_BackgroundLevel_Top_0.getHeight();			
		m_BackgroundLevel_Table_RenderPoints.x = 0;
				
		m_BackgroundLevel_Bottom_0_RenderPoints.x = 0;
		m_BackgroundLevel_Bottom_0_RenderPoints.y = height - m_BackgroundLevel_Bottom_0.getHeight();
		m_BackgroundLevel_Bottom_1_RenderPoints.x = 0;
		m_BackgroundLevel_Bottom_1_RenderPoints.y = height - m_BackgroundLevel_Bottom_1.getHeight();
		
		m_BackgroundLevel_Pot_RenderPoints.x = (width - m_BackgroundLevel_Pot.getWidth())/2;
		m_BackgroundLevel_Pot_RenderPoints.y = height - m_BackgroundLevel_Pot.getHeight();
		
		m_BackgroundLevel_Top_0_RenderPoints.x = 0;
		m_BackgroundLevel_Top_0_RenderPoints.y = 0;
		
		m_Table = new Table(8,8);
		m_Table.LoadResourcesAndCalcPositions(context, width/8, 0.0F, m_BackgroundLevel_Table_RenderPoints.x, m_BackgroundLevel_Table_RenderPoints.y, 0F, -(m_BackgroundLevel_Table.getHeight()+m_BackgroundLevel_Table_RenderPoints.y));
		m_Table.RandomizeTable(false);
		int mX,mY,cv;
		cv = 0;
		for(mX=0; mX < 8; mX++)
		{
			for(mY=7; mY >= 0; mY--)
			{
				m_Table.SetFallInAnimation(mY, mX, cv*0.13D, 0.0F, 1000.0F, 800.0F);
				cv++;
			}
			cv-= 7;
		}		
   }
 
   
   @Override protected void onDraw(Canvas canvas) 
   {
	   if(m_LastFrameTime == -1)
		   m_LastFrameTime = System.currentTimeMillis();
	   long currentTime = System.currentTimeMillis();
	   int delta = (int) (currentTime - m_LastFrameTime);
	   m_LastFrameTime = currentTime;
	   m_DeltaTime = delta;
	   m_TotalElapsedTime += delta;
	   Update(delta);
	   Render(canvas);
 
	   invalidate();
   }	
   
   private void Update(int Delta) 
   {
	   switch (m_Scene)
	   {
	   	case LOADING:
	   		
		   break;
		case GAME:
			boolean animationRun1,animationRun2;
			animationRun1 = m_Table.UpdateSwapAnimation( m_DeltaTime/1000.0F);		
			animationRun2 = m_Table.UpdateFallAnimations(m_DeltaTime/1000.0F);
			
			if(m_CallTouch)
			{
				
				m_CallTouch = false;
				if(!animationRun1 && !animationRun2)
				{
					m_Table.SetSwapPoint(m_TouchX, m_TouchY, m_TouchMode);
				}
			}			
			break;
		case MENU:
			
			break;
		default:
			break;
		}	   
   }

   private void Render(Canvas canvas) 
   {
	   switch (m_Scene) 
	   {
	   	case LOADING:

			break;
		case GAME:
	   		canvas.drawBitmap(m_BackgroundLevel_Table, m_BackgroundLevel_Table_RenderPoints.x, m_BackgroundLevel_Table_RenderPoints.y, null);
	   		m_Table.RenderElements(canvas);
	   		canvas.drawBitmap(m_BackgroundLevel_Bottom_0, m_BackgroundLevel_Bottom_0_RenderPoints.x, m_BackgroundLevel_Bottom_0_RenderPoints.y, null);
	   		canvas.drawBitmap(m_BackgroundLevel_Bottom_1, m_BackgroundLevel_Bottom_1_RenderPoints.x, m_BackgroundLevel_Bottom_1_RenderPoints.y, null);
	   		canvas.drawBitmap(m_BackgroundLevel_Pot, m_BackgroundLevel_Pot_RenderPoints.x, m_BackgroundLevel_Pot_RenderPoints.y, null);
	   		canvas.drawBitmap(m_BackgroundLevel_Top_0, m_BackgroundLevel_Top_0_RenderPoints.x, m_BackgroundLevel_Top_0_RenderPoints.y, null);
	   		
	   		if(m_TotalElapsedTime%10 == 0)
	   		{
	   			m_FPSString = Float.toString(1000.0F/m_DeltaTime);
	   		}
	   		Paint paint = new Paint();
	   		paint.setColor(Color.WHITE);
	   		paint.setTextSize(20);
	   		canvas.drawText(m_FPSString, 300, 25, paint);
	   		
			break;
		case MENU:
			
			break;
		default:
			break;
		}
   }
	   
}
